package servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/online")
public class OnlineServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //创建session对象
        HttpSession session = req.getSession();


        String key = req.getParameter("user");


        if (key != null && "loginOut".equals(key)){
            session.invalidate();
            return;
        }




        //得到当前作用域中的在线人数
        Integer onlineNumber = (Integer) session.getAttribute("onlineNumber");
        //设置响应编码
        resp.setContentType("text/html;charset=UTF-8");

        //输出人数
        resp.getWriter().write("<h2>当前在线人数："+onlineNumber+"<h2>");
    }
}
